package com.example.shanguigu.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

import static com.example.shanguigu.nio.Constants.BASE_DIR;

public class NIOFileChannel02 {

    public static void main(String[] args) {
        try (FileInputStream fos=new FileInputStream(BASE_DIR+"nio_out.txt");
             FileChannel fc= fos.getChannel()){

            ByteBuffer buffer = ByteBuffer.allocate(1024);
            fc.read(buffer);

            buffer.flip();
            String s = StandardCharsets.UTF_8.decode(buffer).toString();
            System.out.println("s = " + s);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
